import request from './request'

import demo from './modules/demo'
import header from './modules/header'
import blog from './modules/blog'
import main from './modules/main'

const modules = {
  ...demo,
  ...header,
  ...blog,
  ...main
}

const api = (type, url, params = {}, options = {}) => {
  if (type === 'get') {
    return request({
      url,
      method: 'get',
      params,
      ...options
    })
  } else if (type === 'post') {
    return request({
      url,
      method: 'post',
      data: params,
      ...options
    })
  }
}

const baseHost = window.g.baseUrl + ''

const result = {}

for (const module in modules) {
  const methods = modules[module]
  result[module] = {}
  for (const method in methods) {
    const { type, path, host, headers = {} } = methods[method]
    result[module][method] = (parmas = {}, options = {}) => {
      const url = (options.host || host || baseHost) + path
      return api(type, url, parmas, { ...options, headers: { ...headers, ...options.headers }, host: null })
    }
  }
}

export default result
